package decision_tree;
import java.io.*;
import java.util.*;
import java.*;


 
public class tree {

	public static double log2(double num)
	{
	return (Math.log(num)/Math.log(2));
	}
	
	    public static void main(String[] args) throws IOException {
	    	String class_label,up="UP";
	       	int up_count=0,down_count=0;
	        Scanner s = null;
	        
	        double attri_up[]=new double[8];
	        double attri_down[]=new double[8];
	        
	        double m_up[]=new double[8];
	        double m_down[]=new double[8]; 
	        
	        double d[]=new double[8];
	        double cont_table[][]=new double[8][4];
	        
	        double training_data[][]=new double[27][9];
	        double h_attri[]=new double[8];
	        double p_up,p_down,info_up_down;
	        double h[]=new double[8];
	        
	        int i=0,j,k=0;
	        try {
	            s = new Scanner(
	                      new BufferedReader(
	                        new FileReader("input_data/in1.txt")));
	            //---------to read each word from input file-------
	            	            
	            while (s.hasNext()) 
	            {
	                j=0;
	            	//System.out.println(s.next());
	            	class_label=s.next();
	            		//System.out.println(class_label);
	            	
	            	if(class_label.equals(up))
	            	{
	            		up_count++;
	            		training_data[i][j]=1;
	             	}
	            	else
	            	{
	            		down_count++;
	            		training_data[i][j]=0;
	            	}
	            	for(j=1;j<9;j++)
	            	{
	            		training_data[i][j]=s.nextDouble();
	            	}
	            	i++;
	            
	            
	            }
	        } finally {
	            if (s != null) {
	                s.close();
	            }
	            
	            
	          /*  for(i=0;i<27;i++)
	            {	for(j=0;j<8;j++)
		             		System.out.print(training_data[i][j]+"\t");
	            	System.out.print("\n");
		        }
	      */
	    //-----calculating total of attribute's values for UP n DOWN separetly
	            
	           for(i=0;i<27;i++)
	           {k=0;	   
		        	for(j=1;j<9;j++)
		        	{
		        		if(training_data[i][0]==1)
		        		{
		        			
		        			attri_up[k++] +=training_data[i][j];
		        		}
		        		else
		        		{
		        			
		        			attri_down[k++] +=training_data[i][j];
		        		}	
		        	}
	           }
	         /*  for(i=0;i<8;i++)
	           {   
	        	   System.out.print (attri_up[i]+"\t");
	        	   System.out.print (attri_down[i]+"\t");
	        	   System.out.println("****");
	           }
	           */
	          
	           
	      //---finding MEAN for all class labels of each attribute separetly
	        	
	           for(i=0;i<8;i++)
	           {   
	        	   m_up[i]=attri_up[i]/up_count;
	        	   m_down[i]=attri_down[i]/down_count;
	           }	
	           
	          /* for(i=0;i<8;i++)
	           {   
	        	   System.out.println (m_up[i]+"\t");
	        	   System.out.println (m_down[i]+"\t\n");
	           }
	        */
	           
	       //---finding (d(i)) total mean for each attribute 	
	        	for(i=0;i<8;i++)
	        	{
	        		
	        		d[i]=(m_up[i]+m_down[i])/2;
	        		
	        	}
	        
	        	for(i=0;i<8;i++)
	        		System.out.println (d[i]+"******\t"); 
 			
	        	
	        //---------contingency for each attribute	
	        	
	        	for(i=0;i<8;i++)
	        		for(j=0;j<4;j++)
	        			cont_table[i][j]=0;
	        	
	        	
	        	for(i=0;i<27;i++)
	        	{
	        		for(j=0;j<8;j++)
	        	
		        	{
		        		if(training_data[i][0]==1)
		        		{	
		        			if(training_data[i][j+1]<=d[j])
		        			{	cont_table[j][0]++ ;}
		        			
		        			else
		        			{	cont_table[j][1]++;}
		        	
		        		}
		        		else
		        		{
		        			if(training_data[i][j+1]<=d[j])
		        			{	cont_table[j][2]++ ;}
		        			
		        			else
		        			{	cont_table[j][3]++;}
		        		}
		        	 }
	        		
	        	}
	        	 		
	        		
	        
		        for(i=0;i<8;i++)
	        		for(j=0;j<4;j++)
	        		{
	        			cont_table[i][j]=cont_table[i][j]/(up_count+down_count);
	        		}
		        
        		for(i=0;i<8;i++)
	        	{	for(j=0;j<4;j++)
	        		{		System.out.print (cont_table[i][j]+"\t");	}
	        		System.out.println("");
	        	}
	        	
	        	
	        	
		        p_up=cont_table[0][0]+cont_table[0][1];
    			//System.out.print(p_up+"\t");
		        p_down=cont_table[0][2]+cont_table[0][3];
    			//System.out.print(p_down+"\t");
		        info_up_down=(double)(p_up*log2((double)(1/p_up))+ p_down*log2((double)(1/p_down)));
    			//System.out.print(info_up_down+"\t");
		        
	        	for(i=0;i<8;i++)
	        	{
	        		        		
	        	//System.out.print((cont_table[i][0]*log2((double)(1/(double)cont_table[i][0]))+cont_table[i][2]*log2((double)(1/(double)cont_table[i][2])))+"\t");
	        	
	        	//System.out.print(cont_table[i][1]*(log2(1.0)-log2(cont_table[i][1]))+cont_table[i][3]*(log2(1.0)-log2(cont_table[i][3]))+"\t");
	        		
	        	h[i]=(double)(info_up_down +cont_table[i][0]*(log2(1.0)-log2(cont_table[i][0]))+cont_table[i][2]*(log2(1.0)-log2(cont_table[i][2]))+cont_table[i][1]*(log2(1.0)-log2(cont_table[i][1]))+cont_table[i][3]*(log2(1.0)-log2(cont_table[i][3])));
	        	//System.out.print(h[i]+"\t");
	        	}
	        	
	        	/*for(i=0;i<8;i++)
	        		System.out.print (h[i]+"\t");
	        	*/	
	        	
	        	
	        	       	
            }

	        
        }
	        

}